package com.example.myapplication.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentHomeBinding;
import com.example.myapplication.ui.home.entity.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    private RecyclerView recyclerView;
    private AdsAdapter adsAdapter;
    private List<Data> adsList;

    private ViewPager viewPager;
    private BannerAdapter adapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textHome;
        homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

        final RecyclerView recyclerViewAds = binding.recyclerViewAds;
        recyclerView = recyclerViewAds.findViewById(R.id.recycler_viewAds);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        adsList = new ArrayList<>();
        // 填充广告数据
        adsList.add(new Data(1L,"第一章 职业理念","职业理念",""));
        adsList.add(new Data(2L,"第二章 职业道德","职业道德",""));
//        adsList.add(new Data(3L,"第三章 教育法律和法规","教育法律和法规",""));
//        adsList.add(new Data(4L,"第四章 基本能力","基本能力",""));
        // ...添加更多广告URL

        adsAdapter = new AdsAdapter(adsList);
        recyclerView.setAdapter(adsAdapter);

        viewPager = binding.viewPager;
        List<Integer> images = Arrays.asList(
                R.drawable.icon_20240523213151,
                R.drawable.icon_20240523213241,
                R.drawable.icon_20240523213328);
        adapter = new BannerAdapter(getActivity(), images);
        viewPager.setAdapter(adapter);
        // 可选：添加自动轮播
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                int current = viewPager.getCurrentItem();
                viewPager.setCurrentItem(current + 1);
                new Handler().postDelayed(this, 3000); // 每3秒轮播一次
            }
        }, 3000);

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}